<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/6/13
 * Time: 11:29
 * 套餐
 */

namespace app\admin\controller;
use think\Request;
use think\Db;

class Padaccount extends Auto{

    public function index(){
        $result  = Db::table('organization')
            ->where('org_parent_code',session('user.org_code'))
            ->select();
        $this->assign('org_list',$result);
        return $this->fetch();
    }

    public function add(){
        $data = [
            'title'=>input('title'),
            'name'=>input('name'),
            'type'=>input('type'),
            'desc'=>input('desc'),
            'status'=>input('status'),
            'price'=>input('price'),
            'pwd'=>'123456',
            'create_time'=>date('Y-m-d H:i:s',time()),
            'org_code'=>session('user.org_code'),

        ];
        $result  = Db::table('padaccount')->insert($data);
        if($result){
            $resultId = Db::table('padaccount')->getLastInsID();
            return $resultId;
        }else{//添加失败
            return 0;
        }
    }

    public function sel(){
        $data = [
            'id'=>input('ID')
        ];
        $result  = Db::table('padaccount')
            ->where($data)
            ->select();
//        var_dump($result);
        if($result){
//            $resultId = Db::table('padaccount')->getLastInsID();
            return json($result);
        }else{//添加失败
            return 0;
        }
    }

//    查询套餐列表
    public function query(){
        $join = [
            ['organization w','a.org_code=w.org_code'],
        ];
        $result  = Db::table('padaccount')
            ->where('org_code',session('user.org_code'))
//            ->where('org_code','ORG_5b208b6aed063')
            ->alias('a')
//            ->join($join)
            ->select();
        $dataarr = [
            'data'=>$result
        ];
        echo json_encode($dataarr);
    }

//    删除
    public function del(){
        $Request = Request::instance();
        $param = $Request->param();
        if($this->querystatus($param['ID'])){
            $ID = $param['ID'];
            $result  = Db::table('padaccount')->delete($ID);
            echo $result;
        }else{
            echo 'error';
        }

    }

//    查询入库设备状态
    public function querystatus($id){
        try{
            $result  = Db::table('padaccount')
                ->where(['id'=>$id,'status'=>'1'])
                ->select();

            if($result){
                return true;
            }else{
                return false;
            }

        }catch (Exception $e){
            echo '服务器内部错误';
        };


    }

//    分配

    /**
     * 修改设备属性
     * 修改套餐属性
     */
    public function distribution(){
//        Db::startTrans();
        Db::startTrans();
        try{
            $Request = Request::instance();
            $param = $Request->param();
            $devid = $param['devid'];
            $org_code = $param['org_code'];
            $id =$param['id'];

             foreach ($devid as $k=>$v){
                 Db::table('device')->where('dev_sn',$v)->update(['org_code'=>$org_code]);
             }
             Db::table('padaccount')->where('id',$id)->update(['org_code'=>$org_code]);
             // 提交事务
             Db::commit();
             echo 1;
        } catch (\Exception $e) {
            var_dump($e);
            // 回滚事务
            Db::rollback();
        }

    }

    public function distributionorg(){
        $Request = Request::instance();
        $param = $Request->param();
        $org_code = $param['org_code'];
        $id =$param['id'];
        $code = Db::table('padaccount')->where('id',$id)->update(['org_code'=>$org_code]);
        echo $code;
    }

//    添加教练
    public function addjl(){
        $Request = Request::instance();
        $param = $Request->param();
//        $devid = $param['devid'];
        $id =$param['ID'];
        $user_name =$param['user_name'];
        $row = Db::table('padaccount')->where('id',$id)->update(['user_name'=>$user_name]);
        return $row;
    }

//    修改账号状态
    public function updstatus(){
        $data = [
            'status'=>input('status')
        ];
        $result  = Db::table('padaccount')
            ->where('id',input('ID'))
            ->update($data);
        echo $result;
    }

//    修改账号属性
    public function updpadacc(){
        $data = input();
        $id = $data['id'];
        unset($data['id']);
        $result  = Db::table('padaccount')
            ->where('id',$id)
            ->update($data);
        echo $result;
    }



}